/**
 * @author Linrui
 * @since 2011-03-07
 * 新闻资讯的业务层封装类
 */
package com.qeado.news.service.implement;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.news.dao.NewsDao;
import com.qeado.news.model.News;
import com.qeado.news.service.NewsService;
import com.qeado.util.FileUpload;
import com.qeado.util.PageHelper;

@Service
public class NewsServiceImplement implements NewsService {
	
	@Autowired
	private NewsDao newsDao;

	public List<News> listAll() {
		return newsDao.listAll();
	}

	public PageHelper listHotByType(int pageNum, int pageSize, String typeCode) {
		return newsDao.listHotByType(pageNum, pageSize, typeCode);
	}

	public PageHelper listByUserName(int pageNum, String userName) {
		return newsDao.listByUserName(pageNum, userName);
	}

	public PageHelper listByPage(int pageNum) {
		return newsDao.listByPage(pageNum);
	}

	public PageHelper listBySortByPage(int pageNum, String typeCode) {
		return newsDao.listBySortByPage(pageNum, typeCode);
	}

	public News queryById(String id) {
		return newsDao.queryById(id);
	}
	
//	public int queryCountBySort(String sortID) {
//		return newsDao.queryCountBySort(sortID);
//	}

	public PageHelper listByUser(int pageNum, int pageSize, String userName,
			String orderby, String asc) {
		if( pageSize == 10 || pageSize == 20 || pageSize == 50 || pageSize == 100) {	
			if( ( !"".equals(userName) && userName!=null ) || ( !"".equals(orderby) && orderby!=null ) || ( !"".equals(asc) && asc!=null ) ) {
				return newsDao.listByUser(pageNum, pageSize, userName, orderby, asc );
			} else 
				return null;			
		} else
			return null;
	}

	/* 发布新闻纪录 */
	public String insertNews(News news) {
		news.setId(UUID.randomUUID().toString());
		if( newsDao.insertNews(news) )
			return "success";
		else
			return "fail";
	}
	public String insertNews(News news, FileUpload upload) {
		if( upload == null){
			news.setFileName("default/default.gif");
			return insertNews(news);
		}
		else {
			upload.upload();
			news.setFileName(upload.getImageFileName());
			return insertNews(news);
		}
	}

	public PageHelper listByTypeEqual(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc) {
		if( pageSize != 0) {
			return newsDao.listByTypeEqual(pageNum, pageSize, typeCode, orderBy, asc);
		} else
			return null;
	}

	public PageHelper listByTypeLike(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc) {
		if( pageSize != 0) {
			typeCode += "%";
			return newsDao.listByTypeLike(pageNum, pageSize, typeCode, orderBy, asc);
		} else
			return null;
	}
	
	
}
